//
//  CourseStudyCell.m
//  ShowYork
//
//  Created by hxh on 2020/9/1.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "LessonStudyItemCell.h"
#import "ExercisesPageModel.h"
#import "ExercieseQuestionItemModel.h"
@interface LessonStudyItemCell ()
@property (nonatomic,strong)UIImageView *coverImgView;
@property (nonatomic,strong)UIImageView *headerImgView;
@property (nonatomic,strong)UIImageView *iconImgView;
@property (nonatomic,strong)UILabel *nameLb;
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UILabel *contentLb;
@property (nonatomic,strong)UIButton *videoBtn;
@property (nonatomic,strong)UILabel *paperLb;
@property (nonatomic,strong)UIButton *studyBtn;
@property (nonatomic,strong)UILabel *progressLb;
@property (nonatomic,strong)UIImageView *progressImgView;
@property (nonatomic,strong)UIImageView *paperImgView;

@property (nonatomic,strong)UIView *coverLine;
@property (nonatomic,strong)UIImageView *nextImgView;
@property (nonatomic,strong)UIImageView *showVideoImgView;

@property (nonatomic,strong)UIButton *exercieseBtn;

@property (nonatomic,strong)UIButton *coverBtn;

@property (nonatomic,strong)LessonPlanModel *planModel;
@end

@implementation LessonStudyItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ( self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.coverBtn];
    
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
    

    self.coverImgView = [[UIImageView alloc]init];
    self.coverImgView.layer.cornerRadius = 5;
    self.coverImgView.layer.masksToBounds = YES;
    self.coverImgView.backgroundColor = RGBA(221, 221, 221, 1);
    [self.contentView addSubview:self.coverImgView];
       
    // 设置默认宽高比
    CGFloat rate = 70 / 125.0;
       
    [self.coverImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.contentView).offset(15);
        make.width.mas_equalTo(KScaleWidth(125));
        make.height.mas_equalTo(KScaleWidth(125) * rate).priorityHigh();
    }];
       
    self.iconImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"course_tag_chinese"]];
    [self.contentView addSubview:self.iconImgView];
       
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.coverImgView).offset(3);
        make.left.equalTo(self.coverImgView.mas_right).offset(15);
    }];
       
    self.contentLb = [[UILabel alloc]init];
    self.contentLb.font = font(12);
    self.contentLb.textColor = C102;
    [self.contentView addSubview:self.contentLb];
    
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.iconImgView);
        make.right.equalTo(self.contentView).offset(-16);
    }];
    
    
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.font = font(14);
    self.titleLb.textColor = RGBA(51, 51, 51, 1);
    [self.contentView addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImgView.mas_bottom).offset(2);
        make.left.equalTo(self.iconImgView);
        make.right.equalTo(self.contentView).offset(-8);
    }];
       
    self.headerImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"defaut_header"]];
    self.headerImgView.layer.cornerRadius = 10;
    self.headerImgView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.headerImgView];
    [self.headerImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.bottom.equalTo(self.coverImgView);
        make.left.equalTo(self.iconImgView);
    }];
       
    self.nameLb = [[UILabel alloc]init];
    self.nameLb.textColor = RGBA(153, 153, 153, 1);
    self.nameLb.font = font(12);
    [self.contentView addSubview:self.nameLb];
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerImgView.mas_right).offset(8);
        make.bottom.equalTo(self.headerImgView);
    }];
    
    self.coverLine = [[UIView alloc]init];
    self.coverLine.backgroundColor = RGBA(230, 230, 230, 1);
    [self.contentView addSubview:self.coverLine];
    
    [self.coverLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-16);
        make.top.equalTo(self.coverImgView.mas_bottom).offset(16);
    }];
    
    self.paperImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"course_after_tag"]];
    [self.contentView addSubview:self.paperImgView];
    
    [self.paperImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.coverImgView);
        make.top.equalTo(self.coverLine.mas_bottom).offset(16);
        make.size.mas_equalTo(CGSizeMake(15,15)).priorityHigh();
        make.bottom.equalTo(self.contentView).offset(-16);
    }];
    
    self.paperLb = [[UILabel alloc]init];
    self.paperLb.text = @"课后练习";
    self.paperLb.textColor = RGBA(51, 51, 51, 1);
    self.paperLb.font = font(14);
    [self.contentView addSubview:self.paperLb];
    
    [self.paperLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.paperImgView.mas_right).offset(10);
        make.centerY.equalTo(self.paperImgView);
    }];
    
    self.studyBtn = [[UIButton alloc]init];
    [self.studyBtn setTitle:@"开始练习" forState:UIControlStateNormal];
    [self.studyBtn setTitleColor:C153 forState:UIControlStateNormal];
    self.studyBtn.titleLabel.font = font(12);
    [self.contentView addSubview:self.studyBtn];
    
    [self.studyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(30);
        make.centerY.equalTo(self.paperLb);
        make.right.equalTo(self.contentView).offset(-15);
    }];
    
    self.videoBtn = [[UIButton alloc]init];
    [self.videoBtn setBackgroundImage:[UIImage imageNamed:@"video_stop"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.videoBtn];
    [self.videoBtn addTarget:self action:@selector(videoMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.videoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.studyBtn);
        make.bottom.equalTo(self.nameLb);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
   
    
    self.nextImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"next_right_tag"]];
    [self.contentView addSubview:self.nextImgView];
    [self.nextImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-16);
        make.centerY.equalTo(self.paperImgView);
        make.size.mas_equalTo(CGSizeMake(6, 12));
    }];
    
    self.showVideoImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"course_video_progress"]];
    self.showVideoImgView.hidden = YES;
    [self.contentView addSubview:self.showVideoImgView];
    [self.showVideoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLb.mas_right).offset(12);
        make.bottom.equalTo(self.nameLb);
    }];

    
    self.progressLb = [[UILabel alloc]init];
    self.progressLb.textColor = [RPAppManager themColor];
    self.progressLb.font = font(12);
    [self.contentView addSubview:self.progressLb];
    
    [self.progressLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.nameLb );
        make.left.equalTo(self.showVideoImgView.mas_right).offset(8);
    }];
    
    self.exercieseBtn = [[UIButton alloc]init];
    [self.exercieseBtn addTarget:self action:@selector(exercieseMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.exercieseBtn];
    [self.exercieseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.coverLine.mas_bottom);
        make.left.bottom.right.equalTo(self.contentView);
    }];

}
+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"LessonStudyItemCell";
    LessonStudyItemCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[LessonStudyItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)setPlanModel:(LessonPlanModel *)planModel andPaperModel:(ExercisesPageModel *)paperModel{
    _planModel = planModel;
    
    self.nameLb.text = planModel.teacherName;
    NSMutableParagraphStyle*style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    //对齐方式
    style.alignment = NSTextAlignmentLeft;
    style.headIndent = 0.0f;//行首缩进进
    style.tailIndent = 0.0f;//行尾缩进
    style.lineBreakMode = NSLineBreakByTruncatingTail;
    //首行缩进
    style.firstLineHeadIndent= self.iconImgView.image.size.width;
    NSAttributedString*attrText = [[NSAttributedString alloc] initWithString:planModel.intro attributes:@{NSParagraphStyleAttributeName: style}];
    self.contentLb.attributedText = attrText;
    self.titleLb.text = planModel.title;
    [self.coverImgView sd_setImageWithURL:[NSURL URLWithString:planModel.cover]];
    [self.headerImgView sd_setImageWithURL:[NSURL URLWithString:planModel.head] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
    NSInteger progress = planModel.progress.intValue/100;
    self.showVideoImgView.hidden = NO;
    if (progress > 0) {
        self.progressLb.text = [NSString stringWithFormat:@"已学%ld%%",(long)progress];
    }else{
        self.progressLb.text = @"已学0%";
    }
    
    if (planModel.paperId == nil) {
        self.paperLb.text = @"";
        self.studyBtn.hidden = YES;
        self.paperImgView.hidden = YES;
        self.nextImgView.hidden = YES;
        self.coverLine.hidden = YES;
        [self.paperImgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.coverLine.mas_bottom);
            make.size.mas_equalTo(CGSizeZero).priorityHigh();
            make.bottom.equalTo(self.contentView).offset(20);
        }];
    }else{
        self.studyBtn.hidden = NO;
        self.paperImgView.hidden = NO;
        self.nextImgView.hidden = NO;
        self.coverLine.hidden = NO;
        [self.paperImgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.coverLine.mas_bottom).offset(16);
            make.size.mas_equalTo(CGSizeMake(15, 15)).priorityHigh();
            make.bottom.equalTo(self.contentView).offset(-16);
        }];
        
        if (planModel.paperAnswerId.intValue == 0) {
            if (paperModel != nil && paperModel.id.intValue > 0) {
                __block NSInteger maxCount = 0;
                __block NSInteger anserCount = 0;
                [paperModel.resultArray enumerateObjectsUsingBlock:^(ExercieseQuestionItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (obj.type.intValue != 0) {
                        maxCount +=1;
                    }
                    if (obj.isMake) {
                        anserCount +=1;
                    }
                }];
                self.paperLb.text = [NSString stringWithFormat:@"课后练习（%ld/%ld）",(long)anserCount,(long)maxCount];
                [self.studyBtn setTitle:@"继续练习" forState:UIControlStateNormal];
            }else{
                [self.studyBtn setTitle:@"开始练习" forState:UIControlStateNormal];
                self.paperLb.text = [NSString stringWithFormat:@"课后练习（0/%d）",planModel.questionCount.intValue == 0?0:planModel.questionCount.intValue];
            }
            self.progressLb.hidden = NO;
        }else{
            
            if (progress == 100) {
                self.paperLb.text = @"课后练习";
            }else{
                self.paperLb.text = [NSString stringWithFormat:@"课后练习（%d/%d）",planModel.questionCount.intValue == 0?0:planModel.questionCount.intValue,planModel.questionCount.intValue == 0?0:planModel.questionCount.intValue];
            }
            [self.studyBtn setTitle:@"查看报告" forState:UIControlStateNormal];
            self.progressLb.hidden = progress == 100;
            self.showVideoImgView.hidden = progress == 100;
        }

    }
    

    if (planModel.subResultId.intValue == 1) {
        self.iconImgView.image = [UIImage imageNamed:@"course_tag_chinese"];
    }
    if (planModel.subResultId.intValue == 2) {
        self.iconImgView.image = [UIImage imageNamed:@"course_tag_math"];
    }
    if (planModel.subResultId.intValue == 3) {
        self.iconImgView.image = [UIImage imageNamed:@"course_tag_english"];
    }

}

- (void)videoMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedAtCurrentModel:)]) {
        [self.delegate didSelectedAtCurrentModel:self.planModel];
    }
}
- (void)exercieseMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(startExercieseAtCurrentModel:)]) {
        [self.delegate startExercieseAtCurrentModel:self.planModel];
    }
}
- (void)coverMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showCourseDetailAtCurrentModel:)]) {
        [self.delegate showCourseDetailAtCurrentModel:self.planModel];
    }
}
@end
